package com.ddwl.shop.controller;

import cn.hutool.core.util.StrUtil;
import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.common.exception.IllegalException;
import com.ddwl.schema.bo.shop.*;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.shop.DistributionFeeVo;
import com.ddwl.schema.vo.shop.ShopDistributionVo;
import com.ddwl.schema.vo.shop.ShopVo;
import com.ddwl.shop.service.ShopDistributionService;
import com.ddwl.shop.service.ShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @Author hemiaojun
 * @Date 2021/5/6 13:53
 */
@Api(tags = "配送管理")
@RequestMapping("/distribution")
@RestController
public class ShopDistributionController extends BaseController {

    @Resource
    private ShopService shopService;
    @Resource
    private ShopDistributionService shopDistributionService;



    @GetMapping("/cardList/{shopId}")
    @Auth(product= {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("获取店铺的配送方式列表")
    public RestVo<ShopDistributionVo> cardList(@PathVariable("shopId") String shopId){
        checkUser(shopId);
        return RestVo.ok(shopDistributionService.cardList(shopId));
    }

    @GetMapping("/get/{id}")
    @Auth(product= {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("获取不同配送方式的详情")
    public RestVo<ShopDistributionVo> get(@PathVariable("id") String id,
                                          @RequestParam("type") @ApiParam(value = "0快递1同城配送2自提",required = true) Integer type,
                                          @RequestParam("shopId") String shopId){
        checkUser(shopId);
        return RestVo.ok(shopDistributionService.get(id,type));
    }

    @PostMapping("/onOrOff")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("配送方式开启或关闭")
    public RestVo<Integer> onOrOff(@Valid @RequestBody DistributionOnOrOffBo bo){
        checkUser(bo.getShopId());
        return RestVo.ok(shopDistributionService.onOrOff(bo));
    }

    @PostMapping("/selfMention/set")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("自提设置")
    public RestVo<String> setSelfMention(@Valid @RequestBody SelfMentionBo bo){
        checkUser(bo.getShopId());
        return RestVo.ok(shopDistributionService.setSelfMention(bo));
    }

    @PostMapping("/localDelivery/set")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("同城配送设置")
    public RestVo<String> setLocalDelivery(@Valid @RequestBody LocalDeliveryBo bo){
        checkUser(bo.getShopId());
        return RestVo.ok(shopDistributionService.setLocalDelivery(bo));
    }

    @PostMapping("/express/set")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("快递配送设置")
    public RestVo<String> setExpress(@Valid @RequestBody LogisticsBo bo){
        checkUser(bo.getShopId());
        return RestVo.ok(shopDistributionService.setExpress(bo));
    }

    @PostMapping("/fee")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE, BaseEnum.ProductEnum.API, BaseEnum.ProductEnum.FENXIAO})
    @ApiOperation("配送费计算")
    public RestVo<Map<String, DistributionFeeVo>> fee(@Valid @RequestBody List<DistributionFeeBo> boList){
        return RestVo.ok(shopDistributionService.fee(boList));
    }

    @PostMapping("/repair")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("修复之前店铺的配送信息")
    public RestVo repair(){
        shopDistributionService.repair();
        return RestVo.ok();
    }

    private void checkUser(String shopId) {
        ShopVo shopVo;
        if (StrUtil.isEmpty(shopId)) {
            shopVo = shopService.getSimpleShopByUserId(getUserId());
        }else{
            shopVo = shopService.judgeUserHasShop(getUserId(), shopId);
        }
        IllegalException.isNull(shopVo,"您无权操作该店铺数据");
    }

}
